﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class RewardPair
{
    public int value;
    public string valueText;
    public Sprite iconImage;
    public RewardType rewardType;
    public CardPair[] cardPairs;
}

public class RewardManager : UIPanel
{
    public RewardPair[] rewardPairs;
    public PlayerEntityData playerEntityData;

    public GameObject rewardItemPrefab;
    public GameObject itemContainer;

    public Sprite moneyRewardImage;
    public Sprite cardRewardImage;
    
    public int rewardItemCount = 0;
    public bool isNeedBackMapScene = true;
    
    private void Start()
    {
        rewardPairs = new RewardPair[3];
        
        CardPair[] tempCardPair = new CardPair[3];
        tempCardPair[0] = new CardPair()
        {
            belongSlot = -1,
            cardData = ResourceLoader.cardCategory.category[10000],
            cardBelongType = CardBelongType.Drag,
            cardDeckType = DeckType.Action
        };

        tempCardPair[1] = new CardPair()
        {
            belongSlot = -1,
            cardData = ResourceLoader.cardCategory.category[20000],
            cardBelongType = CardBelongType.Drag,
            cardDeckType = DeckType.Action
        };

        tempCardPair[2] = new CardPair()
        {
            belongSlot = -1,
            cardData = ResourceLoader.cardCategory.category[30000],
            cardBelongType = CardBelongType.Drag,
            cardDeckType = DeckType.Action
        };
        
        rewardPairs[0] = new RewardPair()
        {
            value = 20,
            cardPairs = null,
            rewardType = RewardType.Money,
            iconImage = moneyRewardImage,
            valueText = "20"
        };
        
        rewardPairs[1] = new RewardPair()
        {
            value = 30,
            cardPairs = null,
            rewardType = RewardType.Money,
            iconImage = moneyRewardImage,
            valueText = "30"
        };
        
        rewardPairs[2] = new RewardPair()
        {
            value = 1,
            cardPairs = tempCardPair,
            rewardType = RewardType.Card,
            iconImage = cardRewardImage,
            valueText = "获得新卡牌"
        };
        
        GenerateRewardPanel();
    }

    public void GenerateRewardPanel()
    {
        rewardItemCount = 0;
        foreach (RewardPair rewardPair in rewardPairs)
        {
            GameObject rewardItemGameObject = Instantiate(rewardItemPrefab, itemContainer.transform, false);

            RewardItem item = rewardItemGameObject.GetComponent<RewardItem>();
            item.SetRewardData(rewardPair);

            rewardItemCount += 1;
        }
    }

    private void DealWithMoney(int value, GameObject belongItem)
    {
        playerEntityData.money += value;
        
        Destroy(belongItem);
        rewardItemCount -= 1;
        
        GameObject.FindGameObjectWithTag(StaticDataManager.GAME_SESSION_TAG).GetComponent<UIFlusher>().FlushAllUIElement();
    }

    private void DealWithCard(int value, CardPair[] cardPairs, GameObject belongItem)
    {
        // TODO 让UI创建一个新窗口
        UiManager.DisplayRewardCardPanel(value, cardPairs, belongItem);
    }

    private void DealWithRelic(int value, GameObject belongItem)
    {
        rewardItemCount -= 1;
        // TODO 加一个当前的遗物
        GameObject.FindGameObjectWithTag(StaticDataManager.GAME_SESSION_TAG).GetComponent<UIFlusher>().FlushAllUIElement();
    }
    
    
    private void Update()
    {
        if (isActive && Input.GetMouseButtonDown(0))
        {
            foreach (RaycastResult result in OnClickGenerate())
            {
                if (result.gameObject.name.Contains("RewardItem"))
                {
                    RewardItem item = result.gameObject.GetComponent<RewardItem>();
                    item.rewardManager = this;
                    
                    switch (item.rewardType)
                    {
                        case RewardType.Money:
                            DealWithMoney(item.value, result.gameObject);
                            break;
                        case RewardType.Card:
                            DealWithCard(item.value, item.cardPairs, result.gameObject);
                            break;
                        case RewardType.Relic:
                            DealWithRelic(item.value, result.gameObject);
                            break;
                    }
                    
                    break;
                }
            }

            ClosePanelWhenZeroList();
        }

        CloseWithRightClick();
    }

    public void ClosePanelWhenZeroList()
    {
        if (rewardItemCount <= 0)
        {
            CloseCurrentPanel();
        }
    }
    
    public override void CloseCurrentPanel()
    {
        if (isNeedBackMapScene)
        {
            GameObject.FindGameObjectWithTag(StaticDataManager.GAME_SESSION_TAG).GetComponent<GameSession>()
                .BackToMapScene();   
        }
        
        base.CloseCurrentPanel();
    }
}
